//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodePropertyReference"/> class represents a property reference
    /// either static or instanced.
    /// </summary>
    public class CodePropertyReference : CodeValueReference
    {
        private CodeExpression m_target;
        private PropertyInfo m_property;

        /// <summary>
        /// Initializes a new instance <see cref="CodePropertyReference"/> with the property
        /// information.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <remarks>The property should be static.</remarks>
        public CodePropertyReference(PropertyInfo property)
        {
            m_property = property;
        }

        /// <summary>
        /// Initializes a new instance <see cref="CodePropertyReference"/> with the property
        /// information and the target expression to access the property on.
        /// </summary>
        /// <param name="target">The target expression.</param>
        /// <param name="property">The property.</param>
        public CodePropertyReference(CodeExpression target, PropertyInfo property)
        {
            m_target = target;
            m_property = property;
        }

        /// <summary>
        /// Generates the IL needed to get the property value.
        /// </summary>
        /// <param name="gen">The generator to emit with.</param>
        public override void Generate(ILGenerator gen)
        {
            CodeGenerationHelper.GenerateMethodCall(gen, m_target, m_property.GetGetMethod());
        }

        /// <summary>
        /// Generates the IL needed to set the property value.
        /// </summary>
        /// <param name="gen">The generator to emit with.</param>
        /// <param name="value">The value expression to assign to the property.</param>
        public override void GenerateSet(ILGenerator gen, CodeExpression value)
        {
            CodeGenerationHelper.GenerateMethodCall(gen, m_target, m_property.GetSetMethod(), value);
        }

        /// <summary>
        /// Prints a human readable version of a property reference.
        /// </summary>
        /// <param name="cw">The code writer to print the code with.</param>
        public override void PrintCode(CodeWriter cw)
        {
            m_target.PrintCode(cw);
            cw.Write(".");
            cw.Write(m_property.Name);
        }

        /// <summary>
        /// Gets the type of the property that is referenced.
        /// </summary>
        public override Type ResultType
        {
            get { return m_property.PropertyType; }
        }
    }
}